/*
 ============================================================================
 Name        : TP2.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include "hashesuncaracter.h"
#include "hashesdoscaracteres.h"
#include "hashestrescaracteres.h"
#define BYTES_DEL_DIGEST 33
#define MAX_C 127
#define MAX_TRES_CHARS 80

//ESTE ES EL MAIN Q TE CALCULA LO HASHE
//int main(int argc, char **argv) { //arg1 es el archivo
//								  //arg2 cant de bytes
//	char * path = argv[1];
//	char * cant_bytes_c = argv[2]; //en el password
//	int cant_bytes = atoi(cant_bytes_c);
//	if (cant_bytes > 3) return 1; //todo mal
//	FILE * file = fopen(path, "r");
//	FILE * f_write = fopen("/home/juan/Desktop/hashes.txt", "w");
//	char c[cant_bytes + 1];
//	c[cant_bytes] = 0;
//	char *buffer = (char *) malloc(BYTES_DEL_DIGEST);
//	char *buffer_leido = (char *) malloc(BYTES_DEL_DIGEST);
//	memset(buffer, 0, BYTES_DEL_DIGEST);//DEBUG, hay que sacarlo
//	memset(buffer_leido, 0, BYTES_DEL_DIGEST);//DEBUG, hay que sacarlo
//
////	whi	{ // el +1 es por \n
////		strtok(buffer_leido,"\n"); //Remuevo el \n
//
//		for (c[0] = 48; c[0] < 127; c[0]++) {
//			for (c[1] = 48; c[1] < 127; c[1]++) {
//				if (c[1] == 48) fprintf(f_write, "{");
//				for (c[2] = 48; c[2] < 127; c[2]++){
//					get_sum(c, buffer);
//					if (c[2] == 48) fprintf(f_write, "{");
//
//				fprintf(f_write, "\"%s\",\n", buffer);
//				if (c[2] == 126) fprintf(f_write, "}");
//				}
//				if (c[1] == 126) fprintf(f_write, "}");
//			}
//		}
////	}
//
//	free(buffer);
//	free(buffer_leido);
//	return EXIT_SUCCESS;
//}

void password1(char * c, char * buff_leido){
	int i;
	char hash[BYTES_DEL_DIGEST];
	for (i = 0; i < MAX_C; i++) {
		strcpy(hash, hashes[i]);
		if (strcmp(hash, buff_leido) == 0) {
			c[0] = i;
			printf("%c\n", c[0]);
			break;
		}
	}
}

void password2(char * c, char * buff_leido){
	int i, y;
	char hash[BYTES_DEL_DIGEST];
	for (i = 0; i < MAX_C; i++) {
		for (y = 0; y < MAX_C; y++) {
			strcpy(hash, hashesdos[i][y]);
			if (strcmp(hash, buff_leido) == 0) {
				c[0] = i;
				c[1] = y;
				printf("%s\n", c);
				break;
			}
		}
	}
}

void password3(char * c, char * buff_leido){
	int i, y, z;
	char hash[BYTES_DEL_DIGEST];
	for (i = 0; i < MAX_TRES_CHARS - 1; i++) {
		for (y = 0; y < MAX_TRES_CHARS - 1; y++) {
			for (z = 0; z < MAX_TRES_CHARS - 1; z++) {
				strcpy(hash, ((hashestres[i])[y])[z]);
				if (strcmp(hash, buff_leido) == 0) {
					c[0] = i + 48;
					c[1] = y + 48;
					c[2] = z + 48;
					printf("%s\n", c);
					break;
				}
			}

		}
	}
}

int main(int argc, char **argv) { //arg1 es el archivo
								  //arg2 cant de bytes

	char * path = argv[1];
	char * cant_bytes_c = argv[2]; //en el password
	int cant_bytes = atoi(cant_bytes_c);
	if (cant_bytes > 3) return 1; //todo mal
	FILE * file = fopen(path, "r");
	char c[cant_bytes + 1];
	c[cant_bytes] = 0;
	char *buffer_leido = (char *) malloc(BYTES_DEL_DIGEST);
	memset(buffer_leido, 0, BYTES_DEL_DIGEST);//DEBUG, hay que sacarlo

	while (fgets(buffer_leido, BYTES_DEL_DIGEST + 1, file)){ // el +1 es por \n
		strtok(buffer_leido,"\n"); //Remuevo el \n
		if(cant_bytes==1){
			password1(c, buffer_leido);
		}
		if(cant_bytes==2){
			password2(c, buffer_leido);
		}
		if(cant_bytes==3){
			password3(c, buffer_leido);
		}
	}
	free(buffer_leido);
	return EXIT_SUCCESS;
}
